

package com.googlecode.flac4j.format.frame.sub;


import javax.xml.bind.annotation.XmlTransient;


/**
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
@XmlTransient
public abstract class AbstractSubData extends SubData {


    /**
     * Creates a new instance.
     *
     * @param type subFrame type
     */
    public AbstractSubData(final int type, final int orderLength) {
        super();

        if (type >> 6 != 0) {
            throw new IllegalArgumentException(
                "type(" + type + ") >> 6 != 0");
        }

        if (orderLength < 0) {
            throw new IllegalArgumentException(
                "orderLength(" + orderLength + ") < 0");
        }

        if (orderLength >= 6) {
            throw new IllegalArgumentException(
                "orderLength(" + orderLength + ") >= 6");
        }

        this.type = type;
        this.orderLength = orderLength;
        maximumOrder = (int) Math.pow(2, orderLength) - 1;
    }


    // --------------------------------------------------------- getSubFrameType
    @Override
    public int getSubFrameType() {
        return type | order;
    }


    @Override
    public void setSubFrameType(final int subFrameType) {
        order = subFrameType & maximumOrder;
    }


    // ------------------------------------------------------------------- order
    public int getOrder() {
        return order;
    }


    public void setOrder(final int order) {

        if (order >> orderLength != 0) {
            throw new IllegalArgumentException(
                "order(" + order + ") >> orderLength != 0");
        }

        this.order = order;
    }


    /** target SubFrame type. */
    private final int type;


    private final int orderLength;


    private final int maximumOrder;


    private int order;


}

